package com.fan.shop.views.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import com.fan.shop.domain.Item;
import com.fan.shop.domain.StockReq;
import com.fan.shop.domain.StockReqLine;
import com.fan.shop.global.JacksonMapper;
import com.fan.shop.model.userclass.StockReqLineUserClass;
import com.fan.shop.model.userclass.StockReqUserClass;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.StockReqLineService;
import com.fan.shop.services.StockReqService;

@ManagedBean(name = "stockReqsBean")
@RequestScoped
public class StockReqsBean {

	private static Logger logger = Logger.getLogger(StockReqsBean.class);

	@ManagedProperty(value = "#{stockReqService}")
	private StockReqService stockReqService;

	@ManagedProperty(value = "#{stockReqLineService}")
	private StockReqLineService stockReqLineService;

	@ManagedProperty(value = "#{itemService}")
	private ItemService itemService;

	private String selectedRequisitionNo;

	private String tableData;
	private int columnsSize;
	private List<String> columnNames;

	private String selectedValue;

	private List<StockReqLineUserClass> selectedStockReqLines;
	private Map<String, Item> selectedItemsMap;

	private StockReq selectedStockReq;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockReqsBean.logger = logger;
	}

	public StockReqService getStockReqService() {
		return stockReqService;
	}

	public void setStockReqService(StockReqService stockReqService) {
		this.stockReqService = stockReqService;
	}

	public StockReqLineService getStockReqLineService() {
		return stockReqLineService;
	}

	public void setStockReqLineService(StockReqLineService stockReqLineService) {
		this.stockReqLineService = stockReqLineService;
	}

	public String getSelectedRequisitionNo() {
		return selectedRequisitionNo;
	}

	public void setSelectedRequisitionNo(String selectedRequisitionNo) {
		this.selectedRequisitionNo = selectedRequisitionNo;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public String getTableData() {
		ObjectMapper mapper = JacksonMapper.getInstance();

		Set<String> columnNames = new HashSet<String>();
		for (String name : stockReqService.getColumnNames()) {
			columnNames.add(name);

		}
		FilterProvider filters = new SimpleFilterProvider().addFilter(
				"myFilter",
				SimpleBeanPropertyFilter.filterOutAllExcept(columnNames));

		tableData = "\"aaData\": [";
		StringBuffer data = new StringBuffer();

		for (Object obj : stockReqService.getList()) {
			try {
				StockReqUserClass stockReqUserClass = (StockReqUserClass)obj;
				String line = mapper.writer(filters).writeValueAsString(stockReqUserClass);
				data = data.append(line);

			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String tmp = data.toString().replaceAll("\\{[^,:\\{\\}]*:", "[");
		tmp = tmp.replaceAll(",[^,:\\{\\}]*:", ",");

		tableData = tableData + tmp.replace("{", "[").replace("}", "],");
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}

		tableData = tableData + "]";

		return tableData;
	}

	public void setTableData(String tableData) {
		this.tableData = tableData;
	}

	public int getColumnsSize() {
		columnsSize = getColumnNames().size();
		return columnsSize;
	}

	public void setColumnsSize(int columnsSize) {
		this.columnsSize = columnsSize;
	}

	public List<String> getColumnNames() {
		columnNames = stockReqService.getColumnNames();
		return columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public List<StockReqLineUserClass> getSelectedStockReqLines() {
		selectedStockReqLines = new ArrayList<StockReqLineUserClass>();
		selectedItemsMap = new HashMap<String, Item>();
		if (selectedRequisitionNo != null
				&& selectedRequisitionNo.trim().length() > 0) {
			if (stockReqService.hasStockReq(selectedRequisitionNo)) {
				for (StockReqLine line : stockReqService
						.getStockReqLines(selectedRequisitionNo)) {
					selectedStockReqLines
							.add((StockReqLineUserClass) stockReqLineService
									.copy(line));
					selectedItemsMap.put(line.getId().getItemCode(),
							itemService.getItem(line.getId().getItemCode()));
				}
			}
		}
		return selectedStockReqLines;
	}

	public void setSelectedStockReqLines(
			List<StockReqLineUserClass> selectedStockReqLines) {
		this.selectedStockReqLines = selectedStockReqLines;
	}

	public Map<String, Item> getSelectedItemsMap() {
		return selectedItemsMap;
	}

	public void setSelectedItemsMap(Map<String, Item> selectedItemsMap) {
		this.selectedItemsMap = selectedItemsMap;
	}

	public StockReq getSelectedStockReq() {
		if (selectedRequisitionNo != null
				&& selectedRequisitionNo.trim().length() > 0) {
			if (stockReqService.hasStockReq(selectedRequisitionNo)) {
				selectedStockReq = stockReqService
						.getStockReq(selectedRequisitionNo);
			}
		}
		return selectedStockReq;
	}

	public void setSelectedStockReq(StockReq selectedStockReq) {
		this.selectedStockReq = selectedStockReq;
	}

	public String createStockReqAction() {

		return "stock_req.faces";
	}

	public String openStockReqAction() {
		return "stock_req.faces";
	}
}
